/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.stats;

import java.sql.*;
import java.io.*;

import nfse.*;

/**
 * @author bduphoff
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class IPStatsProcessor {

    private String url = "";

    private String user = "";

    private String password = "";

    public IPStatsProcessor(String dbURL, String dbUser, String dbPassword) {
        url = dbURL;
        user = dbUser;
        password = dbPassword;
    }

    public void processClassA(String srcPath, String dstPath, String dateStr, SensorInfo info) {
        processIPs(srcPath, dstPath, dateStr, info, "classa");
    }

    public void processClassB(String srcPath, String dstPath, String dateStr, SensorInfo info) {
        processIPs(srcPath, dstPath, dateStr, info, "classb");
    }

    public void processClassC(String srcPath, String dstPath, String dateStr, SensorInfo info) {
        processIPs(srcPath, dstPath, dateStr, info, "classc");
    }

    private void processIPs(String srcPath, String dstPath, String dateStr, SensorInfo info, String database) {
        try {
            String toolName = NetFSE.getTypeName(info.getToolID()).toLowerCase();
            if (toolName.length() == 0)
                return;

            JDBCDatabase db = new JDBCDatabase(url + database, user, password);
            Statement s = db.createStatement();
            //Statement s2 = db.createStatement();

            // Process Source IP Info
            LineNumberReader in = new LineNumberReader(new FileReader(srcPath));
            String line = in.readLine();
            while (line != null) {
                String[] fields = line.split(",");
                long ip = Long.parseLong(NetFSEUtil.convertIPtoHex(fields[0]), 16);
                String sql = "insert into " + toolName + "_src values ('" + fields[0] + "'," + ip + ",'" + dateStr
                        + "'," + fields[1] + ")";
                try {
                    //System.out.println(sql);
                    s.execute(sql);
                } catch (Exception sqle) {
                    //System.out.println("Failed SQL: '" + sql + "'");
                    sql = "select * from " + toolName + "_src where IP=" + ip + " and Stat_Date='" + dateStr + "'";
                    //System.out.println(sql);
                    ResultSet rs = s.executeQuery(sql);
                    if (rs.next()) {
                        int count = rs.getInt("Count");
                        count += Integer.parseInt(fields[1]);
                        sql = "update " + toolName + "_src set Count=" + count + " where IP=" + ip + " and Stat_Date='"
                                + dateStr + "'";
                        //System.out.println(sql);
                        s.execute(sql);
                    }
                }
                line = in.readLine();
            }
            in.close();

            //          Process Dest IP Info
            in = new LineNumberReader(new FileReader(dstPath));
            line = in.readLine();
            while (line != null) {
                String[] fields = line.split(",");
                long ip = Long.parseLong(NetFSEUtil.convertIPtoHex(fields[0]), 16);
                String sql = "insert into " + toolName + "_dst values ('" + fields[0] + "'," + ip + ",'" + dateStr
                        + "'," + fields[1] + ")";
                try {
                    //System.out.println(sql);
                    s.execute(sql);
                } catch (Exception sqle) {
                    //System.out.println("Failed SQL: '" + sql + "'");
                    sql = "select * from " + toolName + "_dst where IP=" + ip + " and Stat_Date='" + dateStr + "'";
                    //System.out.println(sql);
                    try {
                        ResultSet rs = s.executeQuery(sql);
                        if (rs.next()) {
                            int count = rs.getInt("Count");
                            count += Integer.parseInt(fields[1]);
                            sql = "update " + toolName + "_dst set Count=" + count + " where IP=" + ip
                                    + " and Stat_Date='" + dateStr + "'";
                            //System.out.println(sql);
                            s.execute(sql);
                        }
                    } catch (Exception e2) {
                        e2.printStackTrace();
                    }
                }
                line = in.readLine();
            }
            in.close();

            s.close();
            db.closeConnection();
        } catch (Exception E) {
            E.printStackTrace();
        }
    }
}